﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using He.Web.UI;
using System.Data;

namespace He.Web.manage.weixin
{
    public partial class menu_edit : Manage
    {
        public string action = Enums.ActionEnum.Add.ToString(); //操作类型
        public int id;
        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = GetString("action");
            if (!string.IsNullOrEmpty(_action) && _action == Enums.ActionEnum.Edit.ToString())
            {
                this.action = Enums.ActionEnum.Edit.ToString();//修改类型
                if (!int.TryParse(Request.QueryString["id"] as string, out this.id))
                {
                    ShowParamError();
                    return;
                }
            }

            if (!Page.IsPostBack)
            {
                ddlCategoryBind();
                if (action == Enums.ActionEnum.Edit.ToString())
                {
                    chkLoginLevel("editWxmenu");
                    ShowInfo();
                }
                else
                {
                    chkLoginLevel("addWxmenu");
                }
            }
        }

        #region 赋值操作=================================
        private void ShowInfo()
        {
            BLL.wx_menu bll = new BLL.wx_menu();
            Model.wx_menu model = bll.GetModel(this.id);

            ddlPid.SelectedValue = model.pid.ToString();
            txtName.Text = model.name;
            txtKey.Text = model.key;
            txtUrl.Text = model.url;
            txtSortId.Text = model.sort_id.ToString();
            chkLock.Checked = model.is_lock == 1;
        }
        #endregion

        #region 绑定分类=================================
        private void ddlCategoryBind()
        {
            BLL.wx_menu bll = new BLL.wx_menu();
            DataTable dt = bll.GetList("pid=0").Tables[0];
            this.ddlPid.Items.Clear();
            this.ddlPid.Items.Add(new ListItem("作为顶级菜单", "0"));
            foreach (DataRow dr in dt.Rows)
            {
                string id = dr["id"].ToString();
                string name = dr["name"].ToString().Trim();
                this.ddlPid.Items.Add(new ListItem(name, id));
            }
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            BLL.wx_menu bll = new BLL.wx_menu();
            Model.wx_menu model = new Model.wx_menu();
            model.pid = int.Parse(ddlPid.SelectedValue);
            model.type = txtUrl.Text.Trim() == string.Empty ? API.WeiXin.Enums.MenuType.click.ToString() : API.WeiXin.Enums.MenuType.view.ToString();
            model.name = txtName.Text.Trim();
            model.key = txtKey.Text.Trim();
            model.url = txtUrl.Text.Trim();
            model.sort_id = int.Parse(txtSortId.Text.Trim());
            model.is_lock = chkLock.Checked ? 1 : 0;

            if (bll.Add(model) > 0)
            {
                SaveLogs(string.Format("[微信公众号管理]增加自定义菜单:{0}", model.name));
                return true;
            }
            return false;

        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            if (int.Parse(ddlPid.SelectedValue) == _id)
            {
                ShowError("无法选择自己作为父级菜单！");
                return false;
            }
            BLL.wx_menu bll = new BLL.wx_menu();
            Model.wx_menu model = bll.GetModel(_id);
            model.pid = int.Parse(ddlPid.SelectedValue);
            model.type = txtUrl.Text.Trim() == string.Empty ? API.WeiXin.Enums.MenuType.click.ToString() : API.WeiXin.Enums.MenuType.view.ToString();
            model.name = txtName.Text.Trim();
            model.key = txtKey.Text.Trim();
            model.url = txtUrl.Text.Trim();
            model.sort_id = int.Parse(txtSortId.Text.Trim());
            model.is_lock = chkLock.Checked ? 1 : 0;

            if (bll.Update(model))
            {
                SaveLogs(string.Format("[微信公众号管理]增加自定义菜单:{0}", model.name));
                return true;
            }
            return false;
        }
        #endregion

        

        //保存
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (action == Enums.ActionEnum.Edit.ToString()) //修改
            {
                if (!DoEdit(this.id))
                {
                    ShowError("保存过程中发生错误啦！");
                    return;
                }
                ShowSuccess("修改信息成功啦！", "menu_list.aspx");
            }
            else //添加
            {
                if (!DoAdd())
                {
                    ShowError("保存过程中发生错误啦！");
                    return;
                }
                ShowSuccess("添加信息成功啦！", Request.RawUrl);
            }
        }
    }
}